package server;

import httpMessage.HttpRequest;
import httpMessage.HttpResponse;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class HttpServerThread implements Runnable {

    private Socket socket = null;

    public HttpServerThread( Socket socket ) {
	this.socket = socket;
    }

    // maak bufferedreader aan van socket
    private BufferedReader getReader() throws IOException {
	InputStream in = socket.getInputStream();
	BufferedReader stdIn = new BufferedReader( new InputStreamReader( in ) );
	InetAddress client = socket.getInetAddress();
	System.out.println( client + " connected to Server" );
	System.out.println();
	return stdIn;
    }

    // haal message van inputstream;
    private String getMessage( BufferedReader in ) throws IOException {
	char[] buffer = new char[2048];
	int len = in.read( buffer );
	String value = new String( buffer );
	return value;
    }

    // maak response aan voor request
    private HttpResponse handleRequest( HttpRequest request ) {

	HttpResponse response = new HttpResponse();
	String url = request.getUrl();

	
	
	
	if ( url.equals( "/" ) )
	{
	    url = "index.html";
	    response.setStatuscode( "302 Found" );
	}

	if ( url.startsWith( "/" ) )
	    url = "web" + url;
	else
	    url = "web/" + url;
	
	

	String mime = HttpServer.mimeReader.getMimeType( url );

	response.setMimetype( mime );

	byte[] data = null;
	try {
	    data = loadFile( url );
	    response.setSize( data.length );
	    response.setData( data );
	} catch ( IOException e ) {
	    e.printStackTrace();
	    response.setMimetype( "html/text" );
	    response.setStatuscode( "404 Not Found" );
	    response.setData( "404 Not found ".getBytes() );
	    return response;
	}
	return response;
    }

    private byte[] loadFile( String url ) throws IOException {
	byte[] buffer;
	File file = new File( url );
	DataInputStream readfile = new DataInputStream( new FileInputStream( file ) );
	buffer = new byte[(int) file.length()];

	System.out.println( "File " + url + " is " + file.length() + " bytes long" );

	readfile.read( buffer , 0 , (int) file.length() );
	readfile.close();
	return buffer;
    }

    private void sendResponse( HttpResponse response ) throws IOException {

	OutputStream out = socket.getOutputStream();

	out.write( response.toString().getBytes() );
	out.flush();

	out.write( response.getData() );
	out.flush();

	out.close();
    }

    public void run() {
	try {
	    BufferedReader in = getReader();
	    String message = getMessage( in );

	    System.out.println( message );

	    HttpRequest request = new HttpRequest( message );
	    System.out.println( request );
	    HttpResponse response = handleRequest( request );

	    System.out.println( "Response data size: " + response.getData().length );
	    System.out.println( "Request accepted, sending response" );
	    
	    sendResponse( response );
	    //System.out.println(response);
	    System.out.println( "Response sent" );

	    socket.close();
	    in.close();
	} catch ( IOException e ) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }
}
